﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using System.Web;

namespace YouHP.Pages.BBS
{
    public class UserAdmin:BoardBasePage
    {
        public int MaxFileSize = 1000;
        private UserInfo operateduser;
        protected Int32 operateduid;
        protected string operatedusername;

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected void HandleCommon()
        {
            this.AddLocation("管理用户", "/BBS/UserAdmin.aspx");
            if (UserData.Id <= 0)
            {
                this.AddErrLine("您还没有登录，请点击先登录！");
                this.MsgboxUrl = this.GetUrl("/User/Login.aspx");
                return;
            }
            if (!CanBanPoster())
            {
                this.AddErrLine("您不是管理员，没有权限管理用户！");
                this.MsgboxUrl = this.GetUrl("/User/Login.aspx");
                return;
            }
            operateduid = RequestTools.GetInt("Posterid", -1);
            if (operateduid == -1)
            {
                this.AddErrLine("选择的用户不存在");
                return;
            }
            operateduser = UserBLL.Instance.GetUserInfo(operateduid);
            operatedusername = operateduser.UserName;
            if (operateduser == null)
            {
                this.AddErrLine("选择的用户不存在");
                return;
            }


        }

        protected void HandlePost()
        {
            //TopicInfo topic = ValidateForm();
            //string attachedFileAddressName = null;
            /*
            if (!string.IsNullOrEmpty(topic.TopicFile))
            {
                attachedFileAddressName = Utils.SaveRequestTopicFile(UserData.Id, MaxFileSize);
                if (attachedFileAddressName.Equals(""))
                {
                    AddErrLine("上传文件失败");
                    return;
                }
            }
            topic.TopicFile = attachedFileAddressName;
            if (ErrorCount > 0) return;
            bool succ = TopicBLL.Instance.AddTopic(topic);
            if (succ)
            {
                this.Redirect(string.Format("/BBS/Board.aspx?gid={0}",GroupData.Id));
            }
            else
            {
                this.AddErrLine("操作失败！");
            }
            */

            DoBanUserOperation();
        }


        protected void HandleGet()
        {


        }
        protected bool CanBanPoster( )
        {
            bool retVal = false;

            for (int i = 0; i < moderatorsId.Length; i++)
            {
                if (UserData.Id == moderatorsId[i])
                {
                    retVal = true;
                    break;
                }
            }

            return retVal;
        }
        private void DoBanUserOperation()
        {
            string actions = string.Empty;
            string reason = RequestTools.GetString("reason").Trim();
            //int sendmsg = DNTRequest.GetFormInt("sendmessage", 0);

            switch (RequestTools.GetInt("bantype", -1))
            {
                case 0:
                    //正常状态
                    actions = "解除禁止用户";
                    operateduser.UserTypeId = operateduser.UserTypeId & 0xfeff;
                    UserBLL.Instance.UpdateUserProfile(operateduser);
                    AddMsgLine("已根据积分将用户归组");
                    break;
                case 1:
                    //禁止发言
                    actions = "禁止用户发言";
                    operateduser.UserTypeId = operateduser.UserTypeId | 0x0100;
                    UserBLL.Instance.UpdateUserProfile(operateduser);
                    AddMsgLine("已成功禁止所选用户发言");
                    break;
                /*
                case 2:
                    //禁止发言
                    Users.UpdateUserGroup(operateduid, 5);
                    actions = "禁止用户访问";
                    AddMsgLine("已成功禁止所选用户访问");
                    break;
                */
                default:
                    AddMsgLine("错误的禁止类型");
                    return;
            }

        }
        /*
        TopicInfo ValidateForm()
        {
            TopicInfo info = new TopicInfo();

            info.Title = RequestTools.GetFormString("ttitle");
            info.Body = RequestTools.GetFormString("tbody").Trim();
            info.TopicFile = HttpContext.Current.Request.Files[0].FileName;
            if (info.Title.Length == 0)
            {
                this.AddErrLine("标题不能空！");
                return null;
            }
            if (info.Title.Length > Config.TitleMaxSize)
            {
                this.AddErrLine("标题长度不能超过" + Config.TitleMaxSize + "字符！");
                return null;
            }

            if (info.Body.Length == 0)
            {
                this.AddErrLine("内容不能为空！");
                return null;
            }
            else if (info.Body.Length > Config.TopicMaxSize)
            {
                this.AddErrLine("内容长度不能大于 " + Config.TopicMaxSize + "字符！");
                return null;
            }
            info.Groupid = GroupData.Id;
            info.GroupType = GroupData.GroupType;
            info.PostUserId = this.UserData.Id;
            info.PostUserName = this.UserData.UserName;

            info.PostTime = DateTime.Now;
            info.UpdateTime = DateTime.Now;
            info.Sort = RequestTools.GetFormString("TopicSort");


            return info;
        }
        */
    }
}